#include "systemuipage.h"
#include "ui_systemuipage.h"
#include <QProcess>
#include <QDebug>
SystemUiPage::SystemUiPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SystemUiPage)
{
    ui->setupUi(this);
    QString output;
    QProcess *m_qprocess=new QProcess;

    //启动器图标大小
    m_qprocess->start("gsettings get com.deepin.dde.launcher apps-icon-ratio");
    m_qprocess->waitForFinished();
    output=m_qprocess->readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    output.remove("file://");
    ui->doubleSpinBox->setValue(output.toDouble());
    connect(ui->doubleSpinBox, static_cast<void(QDoubleSpinBox::*)(const QString &)>(&QDoubleSpinBox::valueChanged),[=](const QString &text)
    {
        m_qprocess->start("gsettings set com.deepin.dde.launcher apps-icon-ratio "+text);
    });

    //GTK标题栏高度
    QString tmp1;
    QFile file(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.local/share/deepin/themes/deepin/light/titlebar.ini");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()<< "无法打开"+QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.local/share/deepin/themes/deepin/light/titlebar.ini";
    }
    while(!file.atEnd())
    {
        QByteArray line = file.readLine();
        QString str(line);
        //qDebug()<< str;
        tmp1.append(str);
    }
    tmp1=tmp1.mid(tmp1.indexOf("height=")+7,2);
    ui->spinBox_2->setValue(tmp1.toInt());
    connect(ui->spinBox_2, static_cast<void(QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged),[=](const QString &text)
    {
        m_qprocess->start("/opt/apps/top.yzzi.youjian/files/titlebar.sh "+text);
    });

    //热区
    QSettings *qaq=new QSettings(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/top.yzzi.youjian/setting.qaq", QSettings::IniFormat);
    int i=0;
    QList<QComboBox*> cl;
    cl.append(ui->comboBox_2);
    cl.append(ui->comboBox_3);
    cl.append(ui->comboBox_4);
    cl.append(ui->comboBox_5);
    QStringList cln;
    cln.append("TopLeftShell");
    cln.append("TopRightShell");
    cln.append("LowerLeftShell");
    cln.append("LowerRightShell");

    while (i<4) {
        QStringList sl;

        sl << ""
           << "dde-launcher -s"
           <<"/usr/lib/deepin-daemon/desktop-toggle"
           <<"dbus-send --print-reply --dest=com.deepin.dde.shutdownFront /com/deepin/dde/shutdownFront com.deepin.dde.shutdownFront.Show"
           <<"xset dpms force off"
           <<"dbus-send --session --dest=com.deepin.wm --print-reply /com/deepin/wm com.deepin.wm.PerformAction int32:1";
        int t=0;
        while (t<=sl.size()) {
            if(sl.indexOf(qaq->value(cln[i]).toString()))
            {
                cl[i]->setCurrentIndex(sl.indexOf(qaq->value(cln[i]).toString()));
            }
            t++;
        }
        connect(cl[i], static_cast<void (QComboBox::*)(int index)>(&QComboBox::currentIndexChanged),[=](const int &index)
        {
            qaq->setValue(cln[i],sl[index]);
            m_qprocess->start("killall omd-requ");
            m_qprocess->waitForFinished();
            m_qprocess->startDetached("/opt/apps/top.yzzi.youjian/files/omd-requ/omd-requ");

        });
        i++;
    }
    //dock模式切换
    m_qprocess->start("gsettings get com.deepin.dde.dock display-mode");
    m_qprocess->waitForFinished();
    output=m_qprocess->readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    output.remove("file://");
    if(output=="'classic'")
    {
        ui->comboBox->setCurrentIndex(2);
    }else if(output=="'fashion'")
    {
        ui->comboBox->setCurrentIndex(0);
    }else if(output=="'efficient'")
    {
        ui->comboBox->setCurrentIndex(1);
    }
    connect(ui->comboBox, static_cast<void (QComboBox::*)(int index)>(&QComboBox::currentIndexChanged),[=](const int &index)
    {
        QFileInfo *file=new QFileInfo(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/autostart/omd-classic.desktop");
        switch (index) {
        case 0:
            if(file->exists()==true)
            {
                QFile::remove(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/autostart/omd-classic.desktop");
            }
            m_qprocess->start("gsettings set com.deepin.dde.dock display-mode 'fashion'");
            break;
        case 1:
            if(file->exists()==true)
            {
                QFile::remove(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/autostart/omd-classic.desktop");
            }
            m_qprocess->start("gsettings set com.deepin.dde.dock display-mode 'efficient'");
            break;
        case 2:
            if(file->exists()==false)
            {
                QFile::copy(":/kjfs/omd-classic.desktop",QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/autostart/omd-classic.desktop");
            }
            m_qprocess->start("gsettings set com.deepin.dde.dock display-mode 'classic'");
            break;
        }
       delete file;
    });

    //系统圆角大小
    m_qprocess->start("gsettings get com.deepin.xsettings dtk-window-radius");
    m_qprocess->waitForFinished();
    output=m_qprocess->readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    output.remove("file://");
    ui->spinBox->setValue(output.toDouble());
    connect(ui->spinBox, static_cast<void(QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged),[=](const QString &text)
    {
        m_qprocess->start("gsettings set com.deepin.xsettings dtk-window-radius "+text);
    });


    //dock多屏横跳
    m_qprocess->start("gsettings get com.deepin.dde.dock.mainwindow only-show-primary");
    m_qprocess->waitForFinished();
    output=m_qprocess->readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    output.remove("file://");
    if (output=="false")
    {
        ui->sb2->setChecked(true);
    }else if(output=="true")
    {
        ui->sb2->setChecked(false);
    }
    connect(ui->sb2, &DSwitchButton::checkedChanged, this, [=](bool tmp)
    {
        if(tmp==false)
        {
            m_qprocess->start("gsettings set com.deepin.dde.dock.mainwindow only-show-primary true");
        }else{
            m_qprocess->start("gsettings set com.deepin.dde.dock.mainwindow only-show-primary false");
        }

    });

    //启动器包名搜索
    m_qprocess->start("gsettings get com.deepin.dde.launcher search-package-name");
    m_qprocess->waitForFinished();
    output=m_qprocess->readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    output.remove("file://");
    if (output=="false")
    {
        ui->sb1->setChecked(false);
    }else if(output=="true")
    {
        ui->sb1->setChecked(true);
    }
    connect(ui->sb1, &DSwitchButton::checkedChanged, this, [=](bool tmp)
    {
        if(tmp==false)
        {
            m_qprocess->start("gsettings set com.deepin.dde.launcher search-package-name false");
        }else{
            m_qprocess->start("gsettings set com.deepin.dde.launcher search-package-name true");
        }

    });


    //关闭通知
    m_qprocess->start("gsettings get com.deepin.dde.notifications notifycation-closed");
    m_qprocess->waitForFinished();
    output=m_qprocess->readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    output.remove("file://");
    if (output=="false")
    {
        ui->sb3->setChecked(false);
    }else if(output=="true")
    {
        ui->sb3->setChecked(true);
    }
    connect(ui->sb3, &DSwitchButton::checkedChanged, this, [=](bool tmp)
    {
        if(tmp==false)
        {
            m_qprocess->start("gsettings set com.deepin.dde.notifications notifycation-closed false");
        }else{
            m_qprocess->start("gsettings set com.deepin.dde.notifications notifycation-closed true");
        }

    });

    //通知图标显示
    m_qprocess->start("gsettings get com.deepin.dde.notifications show-icon");
    m_qprocess->waitForFinished();
    output=m_qprocess->readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    output.remove("file://");
    if (output=="false")
    {
        ui->sb4->setChecked(false);
    }else if(output=="true")
    {
        ui->sb4->setChecked(true);
    }
    connect(ui->sb4, &DSwitchButton::checkedChanged, this, [=](bool tmp)
    {
        if(tmp==false)
        {
            m_qprocess->start("gsettings set com.deepin.dde.notifications show-icon false");
        }else{
            m_qprocess->start("gsettings set com.deepin.dde.notifications show-icon true");
        }

    });

    }

SystemUiPage::~SystemUiPage()
{
    delete ui;
}
void SystemUiPage::setTheme(bool isDark,QColor color)
{
    // 菜单图标
    if(isDark){
        // 黑色模式
        QPalette *palette = new QPalette;
        palette->setColor(QPalette::Window,QColor("#282828"));
        palette->setColor(QPalette::Base,QColor("#282828"));
        ui->scrollArea->setPalette(*palette);
    }else {
        // 亮色模式
        QPalette *palette = new QPalette;
        palette->setColor(QPalette::Window,QColor("#FFFFFF"));
        palette->setColor(QPalette::Base,QColor("#FFFFFF"));
        ui->scrollArea->setPalette(*palette);

    }
}
